package com.tanhua.dubbo.api.db;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tanhua.dubbo.api.mapper.QuestionMapper;
import com.tanhua.model.db.Question;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class QuestionApiImpl implements QuestionApi{

    @Autowired
    private QuestionMapper questionMapper;

    @Override
    public Question findByUserId(Long userId) {
        return questionMapper.selectOne(
                Wrappers.<Question>lambdaQuery()
                        .eq(Question::getUserId, userId)
        );
    }

    @Override
    public void save(Question question) {
        Question questionInDB = questionMapper.selectOne(Wrappers
                .<Question>lambdaQuery()
                .eq(Question::getUserId, question.getUserId()));
        if(null != questionInDB) {
            questionInDB.setTxt(question.getTxt());
            questionMapper.updateById(questionInDB);
        }else {
            questionMapper.insert(question);
        }
    }
}
